<template>
  <basic-container>
    <div v-show="showSearchFlag" class="flex-start m-t-20 m-b-10">
      <div class="flex-center">
        <search-product class="m-r-10" title="所属产品" @select="search.productId=$event.ids"></search-product>
        <el-input v-model="search.firmwareName" class="m-r-10" placeholder="请输入" size="small" @keyup.native.enter="query"
                  style="width: 300px;">
          <el-button slot="append" :loading="loading" icon="el-icon-search" size="small" type="primary" @click="query">
            查询
          </el-button>
        </el-input>
      </div>
    </div>
    <div class="flex-center m-b-10">
      <div></div>
      <div class="flex-center">
        <el-button :loading="loading" circle icon="el-icon-refresh" @click="query"></el-button>
        <el-button circle icon="el-icon-search" @click="showSearchFlag=!showSearchFlag"></el-button>
        <el-button type="primary" @click="showAddFlag=true">固件上传</el-button>
      </div>
    </div>
    <el-table v-loading="loading" :data="data" :header-cell-style="{background:'#eee',color:'#000'}" border
              class="my-table"
              style="width: 100%">
      <el-table-column align="center" label="序号" type="index" width="60"/>
      <el-table-column label="固件名称" prop="price" min-width="100">
        <template v-slot="scope">{{ scope.row.firmwareName | defaultValue }}</template>
      </el-table-column>
      <el-table-column label="固件版本号" min-width="100">
        <template v-slot="scope">{{ scope.row.firmwareVersion | defaultValue }}</template>
      </el-table-column>
      <el-table-column label="所属产品" min-width="100">
        <template v-slot="scope">{{ scope.row.productName | defaultValue }}</template>
      </el-table-column>
      <el-table-column label="创建时间" min-width="100">
        <template v-slot="scope">{{ scope.row.createTime | defaultValue }}</template>
      </el-table-column>
      <el-table-column label="操作" width="150">
        <template v-slot="scope">
          <el-button icon="el-icon-remove" size="small" type="text" @click="toRemove(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <app-pagination ref="paginationRef" :page-size="page.size" :total="total" class="m-t-20" @change="pageChange"/>
    <div class="h-100px"></div>

    <el-dialog :visible.sync="showAddFlag" append-to-body title="固件上传" width="550px" destroy-on-close>
      <el-form label-width="120px">
        <el-form-item label="固件名称">
          <el-input style="width: 200px" v-model="form.firmwareName" size="small"/>
        </el-form-item>
        <el-form-item label="固件版本号">
          <el-input style="width: 200px" v-model="form.firmwareVersion" size="small"/>
          <span class="m-l-10" style="color: red">版本号格式: 0.0.0.0 </span>
        </el-form-item>
        <el-form-item label="所属产品">
          <search-product width="200" :show-title="false" @select="form.productId=$event.ids"></search-product>
        </el-form-item>
        <el-form-item label="固件上传">
          <app-file-upload :is-img="false" @change="handleUpload"></app-file-upload>
        </el-form-item>
      </el-form>
      <div class="dividing-line m-t-20 m-b-20"></div>
      <div class="flex-end">
        <el-button size="small" @click="closeAddAndEdit">取消</el-button>
        <el-button :loading="submitLoading" size="small" type="primary" @click="doConfirm">确定</el-button>
      </div>
    </el-dialog>
  </basic-container>
</template>

<script>
import AppPagination from "@/components/business/app-pagination";
import {
  equals,
  getRecords,
  getTotal,
  isListEmpty,
  isStringEmpty,
  msgError,
  isTrue,
  confirmWarning, msgSuccess
} from "@/util/common-util";
import AuthWrapper from "@/components/business/auth-wrapper";
import request from "@/router/axios";
import {deleteRecord} from "@/api/business/filling-team";
import SearchProduct from "@/components/SearchCondition/SearchProduct";
import SearchProductGroup from "@/components/SearchCondition/SearchProductGroup";
import AppFileUpload from "@/components/business/app-file-upload";
import {iotfirmwarePage, iotfirmwareRemove, iotfirmwareSubmit} from "@/api/product/firmware";


export default {
  name: "FirmwareList",
  components: {AppFileUpload, SearchProductGroup, SearchProduct, AuthWrapper, AppPagination},
  data() {
    return {
      page: {
        size: 20,
        current: 1
      },
      total: 0,
      searchDate: '',
      search: {
        firmwareName: '',
      },
      form: {
        firmwareName: '',
        firmwareVersion: '',
        productId: '',
        firmwareUrl: '5555',
      },
      showSearchFlag: true,
      loading: false,
      data: [],
      showAddFlag: false,
      submitLoading: false,
      isEdit: false,
    }
  },
  mounted() {
    this.query()
  },
  methods: {
    handleUpload(e) {
      this.form.firmwareUrl = e.toString()
    },
    pageChange(_page) {
      this.page = _page
      this.query()
    },
    refresh() {
      this.$refs.paginationRef.reset()
    },
    query() {
      this.loading = true
      iotfirmwarePage({...this.page, ...this.search}).then(res => {
        this.data = getRecords(res)
        this.total = getTotal(res)
        this.loading = false
      }).catch(() => {
        this.loading = false
      })
    },
    addNew() {
      this.showAddFlag = true
    },
    doConfirm() {
      if (isStringEmpty(this.form.firmwareName)) return msgError("请输入固件名称")
      if (isStringEmpty(this.form.firmwareVersion)) return msgError("请输入固件版本号")
      if(!/^\d+\.\d+\.\d+\.\d+$/.test(this.form.firmwareVersion)) return msgError("固件版本号格式必须为 0.0.0.0 格式")
      if (isStringEmpty(this.form.productId)) return msgError("请输入产品")
      if (isStringEmpty(this.form.firmwareUrl)) return msgError("请上传固件")
      this.submitLoading = true
      iotfirmwareSubmit(this.form).then(res => {
        this.page.current = 1
        this.query()
        this.closeAddAndEdit()
      }).finally(() => {
        this.submitLoading = false
      })
    },
    toDetail(row) {
     this.$emit('detail', row)
    },
    closeAddAndEdit() {
      this.showAddFlag = false
      this.form.firmwareName = ''
      this.form.firmwareVersion = ''
      this.form.productId = ''
      this.form.firmwareUrl = ''
    },
    toRemove(row) {
      confirmWarning('删除操作', `确定删除固件 ${row.firmwareName} ？`, () => {
        iotfirmwareRemove({ids: row.id}).then(res => {
          this.query()
          msgSuccess()
        })
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.my-table {
  /deep/ .el-button--small {
    padding: 0 5px !important;
    font-size: 14px !important;
  }

  /deep/ td {
    padding: 8px 0 !important;
  }
}
</style>
